<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="竞争单位名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="不良行为名称" prop="behaviorName">
        <el-input
          v-model="queryParams.behaviorName"
          placeholder="请输入不良行为名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处罚单位" prop="punishUnit">
        <el-input
          v-model="queryParams.punishUnit"
          placeholder="请输入处罚单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处罚区域" prop="punishRegion">
        <el-input
          v-model="queryParams.punishRegion"
          placeholder="请输入处罚区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:compete_bad_behavior:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:compete_bad_behavior:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:compete_bad_behavior:remove']"
        >删除</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:compete_bad_behavior:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="compete_bad_behaviorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="竞争单位名称" align="left" header-align="center" prop="enterpriseName" width="210" />
      <el-table-column label="不良行为名称" align="center" prop="behaviorName" />
      <el-table-column label="处罚单位" align="center" prop="punishUnit" />
      <el-table-column label="处罚区域" align="center" prop="punishRegion" />
      <el-table-column label="公示截图" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:compete_bad_behavior:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:compete_bad_behavior:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改不良行为对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="竞争单位名称" prop="enterpriseId">
          <el-select
            v-model="form.enterpriseId"
            filterable
            remote
            reserve-keyword
            placeholder="请选择"
            :remote-method="getCompeteEnterpriseList"
            :loading="loading">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.enterpriseName"
              :value="item.enterpriseId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是何不良" prop="behaviorName"  >
          <el-input type="textarea" v-model="form.behaviorName" placeholder="请输入是何不良" />
        </el-form-item>
        <el-form-item label="处罚单位" prop="punishUnit" >
          <el-input  type="textarea" v-model="form.punishUnit" placeholder="请输入处罚单位" />
        </el-form-item>
        <el-form-item label="处罚区域" prop="punishRegion" >
          <el-input  type="textarea" v-model="form.punishRegion" placeholder="请输入处罚区域" />
        </el-form-item>
        <el-form-item label="公示截图" prop="image" >
          <image-upload v-model="form.image"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCompete_bad_behavior, getCompete_bad_behavior, delCompete_bad_behavior, addCompete_bad_behavior, updateCompete_bad_behavior } from "@/api/business/compete_bad_behavior";
import { listCompete_enterprise} from "@/api/business/compete_enterprise";

export default {
  name: "Compete_bad_behavior",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 不良行为表格数据
      compete_bad_behaviorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
        enterpriseName: null,
        behaviorName: null,
        punishUnit: null,
        punishRegion: null,
        image: null,
        creator: null,
      },
      options: [],
      //竞争企业查询参数
      queryEnterpriseParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
      },
      option: {
        enterpriseId: null,
        enterpriseName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        enterpriseId: [
          { required: true, message: "竞争单位名称不能为空", trigger: "blur" }
        ],
        behaviorName: [
          { required: true, message: "是何不良不能为空", trigger: "blur" }
        ],
        punishUnit: [
          { required: true, message: "处罚单位不能为空", trigger: "blur" }
        ],
        punishRegion: [
          { required: true, message: "处罚区域不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "公示截图不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const tempEnterpriseId = this.$route.params && this.$route.params.enterpriseId;
    if(tempEnterpriseId!=null){
      this.queryParams.enterpriseId = tempEnterpriseId;
    }
    this.getList();
  },
  methods: {
    /** 查询不良行为列表 */
    getList() {
      this.loading = true;
      listCompete_bad_behavior(this.queryParams).then(response => {
        this.compete_bad_behaviorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCompeteEnterpriseList(query) {
      if (query !== '') {
        this.loading = true;
        this.queryEnterpriseParams.enterpriseName = query;
        listCompete_enterprise(this.queryEnterpriseParams).then(response => {
          this.loading = false;
          this.options = response.rows;
        });
      } else {
        this.options = [];
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        behaviorId: null,
        enterpriseId: null,
        behaviorName: null,
        punishUnit: null,
        punishRegion: null,
        image: null,
        creator: null,
        createTime: null,
        updateTime: null,
        options: [],
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.behaviorId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.loading = true;
      listCompete_enterprise(this.queryEnterpriseParams).then(response => {
        this.loading = false;
        this.options = response.rows;
      });
      this.title = "添加竞争单位不良行为";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.option.enterpriseId=row.enterpriseId;
      this.option.enterpriseName=row.enterpriseName;
      this.options.push(this.option);
      const behaviorId = row.behaviorId || this.ids
      getCompete_bad_behavior(behaviorId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改竞争单位不良行为";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.behaviorId != null) {
            updateCompete_bad_behavior(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompete_bad_behavior(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const behaviorIds = row.behaviorId || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delCompete_bad_behavior(behaviorIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/compete_bad_behavior/export', {
        ...this.queryParams
      }, `compete_bad_behavior_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
